package com.ht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.dto.MyDoctor;
import com.ht.mapper.MyDoctorMapper;
import com.ht.service.IMyDoctorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 代齐园
 * @since 2022-05-25
 */
@Service
public class MyDoctorServiceImpl extends ServiceImpl<MyDoctorMapper, MyDoctor> implements IMyDoctorService {

    @Resource
    private MyDoctorMapper myDoctorMapper;

    @Override
    public List<MyDoctor> getMyDoctor(Long id,String status) {
        QueryWrapper<MyDoctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id).eq("status",status);
        List<MyDoctor> list = myDoctorMapper.selectList(queryWrapper);
        return list;
    }
    @Override
    public void insertDoctor(MyDoctor doctor,Long id,Long refinance) {
        doctor.setUserId(id);
        LocalDate localDate = LocalDate.now();//当前时间
        LocalDate localDate1 = localDate.plusDays(refinance);
        doctor.setSeverTime(localDate1);
        myDoctorMapper.insert(doctor);
    }

    @Override
    public void selectExpiration() {
        QueryWrapper<MyDoctor> queryWrapper = new QueryWrapper();
        //查询会员时间=今天的并且为私人医生的
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        queryWrapper.eq("sever_time",format).eq("private_doctor",1);
        List<MyDoctor> list = myDoctorMapper.selectList(queryWrapper);
        list.forEach(myDoctor -> {
            myDoctor.setPrivateDoctor("0");
            myDoctorMapper.updateById(myDoctor);
        });
        System.out.println(format+"定时器完成了更改");
    }

}